package com.cykj.pos.service.impl;

import com.cykj.common.core.domain.entity.SysUser;
import com.cykj.common.utils.SecurityUtils;
import com.cykj.pos.domain.BizAgentOpenAccount;
import com.cykj.pos.domain.BizMerchant;
import com.cykj.pos.domain.dto.UseRegistrationDTO;
import com.cykj.pos.domain.dto.UseRegistrationEntityDTO;
import com.cykj.pos.mapper.BizUserAccountMapper;
import com.cykj.pos.service.IBizAgentOpenAccountService;
import com.cykj.pos.service.IBizMerchantService;
import com.cykj.pos.service.IBizUserAccountService;
import com.cykj.pos.util.DESHelperUtil;
import com.cykj.system.service.ISysUserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 1、统一商户入件Service业务层处理
 *
 * @author weijianbo
 * @date 2021-09-01
 */
@Service
public class BizUserAccountServiceImpl implements IBizUserAccountService {
    @Autowired
    private BizUserAccountMapper bizUserAccountMapper;

    @Resource
    private ISysUserService sysUserService;

    @Resource
    private IBizMerchantService  bizMerchantService;

    @Resource
    private IBizAgentOpenAccountService bizAgentOpenAccountService;

    /**
     * 查询1、统一商户入件
     *
     * @param id 1、统一商户入件ID
     * @return 1、统一商户入件
     */
    @Override
    public UseRegistrationDTO selectBizUserAccountById(Long id) {
        UseRegistrationDTO useRegistrationDTO= bizUserAccountMapper.selectBizUserAccountById(id);
//        if (useRegistrationDTO == null) {
//            System.out.println("空");
//            UseRegistrationDTO u = new UseRegistrationDTO();
//            u.setPhonenumber("");
//            return new UseRegistrationDTO();
//        }
        return useRegistrationDTO;
    }


    @Override
    public UseRegistrationDTO selectBizUserSetId(Long id) {
        UseRegistrationDTO useRegistrationDTO= bizUserAccountMapper.selectBizUserSetId(id);
        return useRegistrationDTO;
    }

    /**
     * 查询1、统一商户入件列表
     *
     * @param useRegistrationDTO 1、统一商户入件
     * @return 1、统一商户入件
     */
    @Override
    public List<UseRegistrationDTO> selectBizUserAccountList(UseRegistrationDTO useRegistrationDTO) {
        UseRegistrationEntityDTO dto = new UseRegistrationEntityDTO();
        dto.setPhonenumber(useRegistrationDTO.getPhonenumber());
        dto.setMerchId(useRegistrationDTO.getParentId());
        dto.setNickName(useRegistrationDTO.getNickName());
        // 获取当前用户信息
        SysUser sysUser= SecurityUtils.getLoginUser().getUser();
        // 如果登录人id = 1， 就直接改成id = 2
        if (sysUser.getUserId() == 1) {
            sysUser.setUserId(2L);
        }
        if (dto.getMerchId() == null) {
            // 查询本人merchId
            dto.setLoginMerchId(bizUserAccountMapper.selectMerchId(sysUser.getUserId()));
        }
        if (useRegistrationDTO.getPageNo() != null && useRegistrationDTO.getPageSize() != null) {
            PageHelper.startPage(useRegistrationDTO.getPageNo(), useRegistrationDTO.getPageSize());
        }
        List<UseRegistrationDTO> list = bizUserAccountMapper.selectBizUserAccountList(dto);
        for (UseRegistrationDTO useRegistrationDTO1:list){
            String profitAmount=DESHelperUtil.decrypt(useRegistrationDTO1.getSecretKey(),useRegistrationDTO1.getProfitAmount());
            String rewardAmount=DESHelperUtil.decrypt(useRegistrationDTO1.getSecretKey(),useRegistrationDTO1.getRewardAmount());
            String coverAmount=DESHelperUtil.decrypt(useRegistrationDTO1.getSecretKey(),useRegistrationDTO1.getCoverAmount());
            String flowAmount=DESHelperUtil.decrypt(useRegistrationDTO1.getSecretKey(),useRegistrationDTO1.getFlowAmount());
            String walletAmount=DESHelperUtil.decrypt(useRegistrationDTO1.getSecretKey(),useRegistrationDTO1.getWalletAmount());
            useRegistrationDTO1.setProfitAmount(profitAmount);
            useRegistrationDTO1.setRewardAmount(rewardAmount);
            useRegistrationDTO1.setCoverAmount(coverAmount);
            useRegistrationDTO1.setFlowAmount(flowAmount);
            useRegistrationDTO1.setWalletAmount(walletAmount);
        }
        return list;
    }
    @Override
    public List<UseRegistrationDTO> exportBizUserAccountList(UseRegistrationDTO useRegistrationDTO) {
        UseRegistrationEntityDTO dto = new UseRegistrationEntityDTO();
        dto.setPhonenumber(useRegistrationDTO.getPhonenumber());
        dto.setMerchId(useRegistrationDTO.getParentId());
        dto.setNickName(useRegistrationDTO.getNickName());
        // 获取当前用户信息
        SysUser sysUser= SecurityUtils.getLoginUser().getUser();
        // 如果登录人id = 1， 就直接改成id = 2
        if (sysUser.getUserId() == 1) {
            sysUser.setUserId(2L);
        }
        if (dto.getMerchId() == null) {
            // 查询本人merchId
            dto.setLoginMerchId(bizUserAccountMapper.selectMerchId(sysUser.getUserId()));
        }
        List<UseRegistrationDTO> list = bizUserAccountMapper.selectBizUserAccountList(dto);
        for (UseRegistrationDTO useRegistrationDTO1:list){
            String profitAmount=DESHelperUtil.decrypt(useRegistrationDTO1.getSecretKey(),useRegistrationDTO1.getProfitAmount());
            String rewardAmount=DESHelperUtil.decrypt(useRegistrationDTO1.getSecretKey(),useRegistrationDTO1.getRewardAmount());
            String coverAmount=DESHelperUtil.decrypt(useRegistrationDTO1.getSecretKey(),useRegistrationDTO1.getCoverAmount());
            String flowAmount=DESHelperUtil.decrypt(useRegistrationDTO1.getSecretKey(),useRegistrationDTO1.getFlowAmount());
            String walletAmount=DESHelperUtil.decrypt(useRegistrationDTO1.getSecretKey(),useRegistrationDTO1.getWalletAmount());
            useRegistrationDTO1.setProfitAmount(profitAmount);
            useRegistrationDTO1.setRewardAmount(rewardAmount);
            useRegistrationDTO1.setCoverAmount(coverAmount);
            useRegistrationDTO1.setFlowAmount(flowAmount);
            useRegistrationDTO1.setWalletAmount(walletAmount);
        }
        return list;
    }
    @Override
    public List<UseRegistrationDTO> filtrationList(UseRegistrationDTO useRegistrationDTO) {
        List<UseRegistrationDTO> list = bizUserAccountMapper.filtrationList(useRegistrationDTO);
        return list;
    }

    /**
     * 新增1、统一商户入件
     *
     * @param sysUser 1、统一商户入件
     * @return 结果
     */
    @Override
    public int insertBizUser(SysUser sysUser) {
        return bizUserAccountMapper.insertBizUser(sysUser);
    }

    /**
     * 新增1、统一商户入件
     *
     * @param bizAgentOpenAccount 1、统一商户入件
     * @return 结果
     */
    @Override
    public int insertBizAccount(BizAgentOpenAccount bizAgentOpenAccount) {
        return bizUserAccountMapper.insertBizAccount(bizAgentOpenAccount);
    }

    /**
     * 修改1、统一商户入件
     *
     * @param sysUser 1、统一商户入件
     * @return 结果
     */
    @Override
    public int updateBizUser(SysUser sysUser) {
        return bizUserAccountMapper.updateBizUser(sysUser);
    }

    /**
     * 修改1、统一商户入件
     *
     * @param bizAgentOpenAccount 1、统一商户入件
     * @return 结果
     */
    @Override
    public int updateBizAccount(BizAgentOpenAccount bizAgentOpenAccount) {
        return bizUserAccountMapper.updateBizAccount(bizAgentOpenAccount);
    }

    @Override
    public int updateBizSetAccount(BizAgentOpenAccount bizAgentOpenAccount) {

        return bizUserAccountMapper.updateBizSetAccount(bizAgentOpenAccount);
    }

    /**
     * 批量删除1、统一商户入件
     *
     * @param ids 需要删除的1、统一商户入件ID
     * @return 结果
     */
    @Override
    public int deleteBizUserAccountByIds(Long[] ids) {
        return bizUserAccountMapper.deleteBizUserAccountByIds(ids);
    }

    /**
     * 删除1、统一商户入件信息
     *
     * @param id 1、统一商户入件ID
     * @return 结果
     */
    @Override
    public int deleteBizUserAccountById(Long id) {
        return bizUserAccountMapper.deleteBizUserAccountById(id);
    }

    @Override
    public BizAgentOpenAccount getBizAgentOpenAccountByMerchId(Long merchId) {
        return bizUserAccountMapper.getBizAgentOpenAccountByMerchId(merchId);
    }

    @Override
    public String getPlusThreeAgentBy(Long accountId) {
        return bizUserAccountMapper.getPlusThreeAgentBy(accountId);
    }

    @Override
    public List<BizMerchant> getAllChildMerchantByAccountId(Long accountId) {
        return bizUserAccountMapper.getAllChildMerchantByAccountId(accountId);
    }

    @Override
    public void updateAccountPlusThreeByUserId(Long userId, String plusThree) {
        bizUserAccountMapper.updateAccountPlusThreeByUserId(userId,plusThree);
    }
}
